﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LDTB.Service;
using System.Globalization;

namespace LDTB
{
    public partial class FormServeyEdit : Form
    {

        LDTBDataDataContext DB = DataService.DB;
        public int peopleid;

        public FormServeyEdit()
        {
            InitializeComponent();
        }

        public FormServeyEdit(int pepleid_)
        {

            InitializeComponent();
            peopleid = pepleid_;

        }

        private void FormServeyEdit_Load(object sender, EventArgs e)
        {
            var pl = (from p in DB.Peoples
                      where p.ID == peopleid
                      select p).FirstOrDefault();

            //khoi tao cap so
            List<ItemExtra> ltCompa = new List<ItemExtra>();

            for (int i = 1; i < 100; i++)
            {
                ltCompa.Add(new ItemExtra { ID = i, Name = i < 10 ? "0" + i.ToString() : i.ToString() });
            }
            ddlCompareNumber.DataSource = ltCompa;
            ddlCompareNumber.DisplayMember = "Name";
            ddlCompareNumber.ValueMember = "ID";
            ddlCompareNumber.SelectedValue = (int)pl.NumberPair.Value;

            //khoi tao loai doi tuong

            List<ItemExtra> ltSex = new List<ItemExtra>();

            ltSex.Add(new ItemExtra { ID = 0, Name = "Nam" });
            ltSex.Add(new ItemExtra { ID = 1, Name = "Nữ" });
            this.ddlSex.DataSource = ltSex;
            this.ddlSex.DisplayMember = "Name";
            this.ddlSex.ValueMember = "ID";
            if (pl.Sex.Value)
            {
                ddlSex.SelectedValue = 0;
            }
            else
            {
                ddlSex.SelectedValue = 1;
            }


            var ward = from w in DB.Wards
                       select w;
            this.ddlWard.DataSource = ward;
            this.ddlWard.ValueMember = "ID";
            this.ddlWard.DisplayMember = "WardName";
            this.ddlWard.SelectedValue = pl.Village.WardID.Value;


            var village = from v in DB.Villages
                          where v.WardID == pl.Village.WardID
                          select v;
            this.ddlVillage.DataSource = village;
            this.ddlVillage.ValueMember = "VillageID";
            this.ddlVillage.DisplayMember = "VillageName";
            this.ddlVillage.SelectedValue = pl.LocationID.Value;

            //khoi tao lua chon cho doi tuong

            var catob = from c in DB.CateObjects
                        select c;
            this.ddlCatObject.DataSource = catob;
            this.ddlCatObject.DisplayMember = "CatName";
            this.ddlCatObject.ValueMember = "CatID";
            this.ddlCatObject.SelectedValue = pl.CatID;

            //khoi tao gia tri
            int id = int.Parse(ddlCatObject.SelectedValue.ToString());
            var catopt = from c in DB.OptionCates
                         where c.CateDisableID == id
                         select c;

            this.ddlCatOp.DataSource = catopt;
            this.ddlCatOp.DisplayMember = "DegreeName";
            this.ddlCatOp.ValueMember = "ID";
            this.ddlCatOp.SelectedValue = pl.DegreeID;

            //khoi tao gia tri


            this.txtFaminiName.Text = pl.FamilyName;
            this.txtFirstName.Text = pl.FirstName;
            this.txtMoneyMonth.Text = pl.ReciveMoneyMonth.Value.ToString();
            this.txtMoneyStartDate.Text = pl.ReciveMoneyStartDate.Value.ToString("MM/yyyy");
            this.txtYearBirthday.Text = pl.BirdthDay.Value.ToString();
            this.chkIsPoor.Checked = pl.IsPoor.Value;
            this.txtAddress.Text = pl.Address;


        }

        public void SaveNewPeople()
        {

            int yearbirth;
            DateTime startDate;
            CultureInfo ci = new CultureInfo("en-IE");

            if (this.txtFaminiName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Nhập họ ");
                txtFaminiName.Focus();

            }
            else if (this.txtFirstName.Text.Trim() == string.Empty)
            {

                MessageBox.Show("Nhập tên");
                this.txtFirstName.Focus();

            }
            else if (this.ddlSex.SelectedIndex == -1)
            {

                MessageBox.Show("Chọn giới tính");
            }
            else if (this.txtYearBirthday.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Nhập năm sinh");
                this.txtYearBirthday.Focus();
            }
            else if (this.txtMoneyMonth.Text.Trim() == string.Empty)
            {

                MessageBox.Show("Nhập số tiền nhận hàng tháng");
                this.txtMoneyMonth.Focus();

            }
            else if (this.txtMoneyStartDate.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Nhập ngày thời điểm nhận lương ");
                this.txtMoneyStartDate.Focus();
            }
            else if (!DateTime.TryParseExact(this.txtMoneyStartDate.Text, "MM/yyyy", ci, DateTimeStyles.None, out startDate))
            {
                MessageBox.Show("Ngày tháng không hợp lệ tháng/năm");
                this.txtMoneyStartDate.Focus();

            }
            else if (!int.TryParse(txtYearBirthday.Text, out yearbirth))
            {

                MessageBox.Show("Năm sinh phải ở dạng số");
                this.txtYearBirthday.Focus();

            }

            else if (yearbirth > 2050)
            {
                MessageBox.Show("Năm sinh nhỏ hơn 2050");
                this.txtYearBirthday.Focus();
            }
            else
            {

                bool sex = (int)ddlSex.SelectedValue == 0 ? true : false;
                //CultureInfo provider = CultureInfo.InvariantCulture;
                //DateTime startDate = DateTime.ParseExact(txtMoneyStartDate.Text, "MM/yyyy", provider);
                try
                {

                    var pl = (from p in DB.Peoples
                              where p.ID == peopleid
                              select p).FirstOrDefault();

                    //CultureInfo provider = CultureInfo.InvariantCulture;
                    //DateTime startDate = DateTime.ParseExact(txtMoneyStartDate.Text, "MM/yyyy", provider);

                    pl.FirstName = txtFirstName.Text.Trim();
                    pl.FamilyName = txtFaminiName.Text.Trim();
                    pl.BirdthDay = int.Parse(txtYearBirthday.Text);
                    pl.CatID = (int)ddlCatObject.SelectedValue;
                    pl.DegreeID = (int)ddlCatOp.SelectedValue;
                    pl.LocationID = (int)ddlVillage.SelectedValue;
                    pl.NumberPair = byte.Parse(ddlCompareNumber.SelectedValue.ToString());
                    pl.Sex = (bool?)sex;
                    pl.Address = this.txtAddress.Text.Trim();
                    pl.ReciveMoneyMonth = int.Parse(txtMoneyMonth.Text);
                    pl.ReciveMoneyStartDate = startDate;
                    pl.IsPoor = chkIsPoor.Checked;

                    DB.SubmitChanges();
                    MessageBox.Show("Sửa thành công");
                }
                catch (Exception e_)
                {

                    MessageBox.Show(e_.Message);
                }
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {

            SaveNewPeople();
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ddlWard_SelectionChangeCommitted(object sender, EventArgs e)
        {
            //khoi tao lang xa
            int wardid = (int)this.ddlWard.SelectedValue;


            var village = from v in DB.Villages
                          where v.WardID == wardid
                          select v;
            this.ddlVillage.DataSource = village;
            this.ddlVillage.ValueMember = "VillageID";
            this.ddlVillage.DisplayMember = "VillageName";


            var pl = (from p in DB.Peoples
                      where p.ID == peopleid
                      select p).FirstOrDefault();

            //gan lang xa ve vij tri dau neu huyen chon dung ve ban dau

            if (pl.Village.WardID == wardid)
            {
                ddlVillage.SelectedValue = pl.LocationID;

            }

        }

        private void ddlCatObject_SelectedIndexChanged(object sender, EventArgs e)
        {
            //khoi tao gia tri
            int id;
            if (ddlCatObject.SelectedValue.GetType().Name == "CateObject")
            {
                id = (ddlCatObject.SelectedValue as CateObject).CatID;
            }
            else
            {
                id = (int)ddlCatObject.SelectedValue;
            }

            var catopt = from c in DB.OptionCates
                         where c.CateDisableID == id
                         select c;

            this.ddlCatOp.DataSource = catopt;
            this.ddlCatOp.DisplayMember = "DegreeName";
            this.ddlCatOp.ValueMember = "ID";
        }
    }
}
